package me.pplay.playtalk.activity;

import me.pplay.playtalk.ApplicationTrans;
import me.pplay.playtalk.R;
import me.pplay.playtalk.protocol.DataCallBack;
import me.pplay.playtalk.protocol.TpAuth;
import me.pplay.playtalk.protocol.bean.RspData;
import me.pplay.playtalk.util.LogManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class SigninGetPwdSecretQuestionActivity extends BaseActivity {
	private ImageButton signin_gewpwd_secretquestion_btn;
	private EditText signin_gewpwd_secretquestion_answer_edit;
	private EditText signin_gewpwd_secretquestion_newpassword_edit;
	private TextView signin_gewpwd_secretquestion_q;
	private String username;
	private TpAuth tpAuth; 
	Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case TpAuth.AUTH_GETPWD_OK: {
				LogManager.LogShow("SigninGetPwdSecretQuestionActivity", "handleMessage AUTH_GETPWD_OK");
				RspData rspdata = (RspData) msg.obj;
				showToast(rspdata.getString() == null ? "Change password ok" : rspdata.getString());
			}
				break;
			case TpAuth.AUTH_GETPWD_ERR: {
				LogManager.LogShow("SigninGetPwdSecretQuestionActivity", "handleMessage AUTH_GETPWD_ERR");
				RspData rspdata = (RspData) msg.obj;
				showToast(rspdata.getString() == null ? "Change password unkown err" : rspdata.getString());
			}
				break;
			case TpAuth.AUTH_GETPWD_TIMEOUT:
				LogManager.LogShow("SigninGetPwdSecretQuestionActivity", "handleMessage AUTH_GETPWD_TIMEOUT");
				showToast("Change Password time out");
				break;
			default:
				break;
			}
		}

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_signin_getpwd_secretquestion);
		ApplicationTrans appState = (ApplicationTrans) getApplicationContext();
		tpAuth = appState.getTpAuth();
		signin_gewpwd_secretquestion_q = (TextView) findViewById(R.id.signin_gewpwd_secretquestion_q);
		signin_gewpwd_secretquestion_newpassword_edit = (EditText) findViewById(R.id.signin_gewpwd_secretquestion_newpassword_edit);
		signin_gewpwd_secretquestion_answer_edit = (EditText) findViewById(R.id.signin_gewpwd_secretquestion_answer_edit);
		signin_gewpwd_secretquestion_btn = (ImageButton) findViewById(R.id.signin_gewpwd_secretquestion_btn);
		signin_gewpwd_secretquestion_btn.setOnClickListener(new BtnSelectedListener());
		username = getIntent().getStringExtra("username");
	}

	class BtnSelectedListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			String answer = signin_gewpwd_secretquestion_newpassword_edit.getText().toString().trim();
			String newpassword = signin_gewpwd_secretquestion_answer_edit.getText().toString().trim();
			if (answer.equals("") || newpassword.equals("")) {
				showToast(getString(R.string.string_isnull));
				return;
			}
			String me_qstr = tpAuth.tp_auth_get_pwdprotection_me_qstr();
			if (me_qstr != null) {
				getPwdBySecretQuestion(me_qstr, answer, newpassword);
			} 
		}
	}


	private void getPwdBySecretQuestion(String qstr, String answer, String newpassword) {
		tpAuth.tp_auth_get_pwdbypwdprotection(username, newpassword, qstr, answer, new DataCallBack() {

			@Override
			public void Callback(int result, RspData resultdata, Object obj) {
				// TODO Auto-generated method stub
				Message msg = new Message();
				msg.what = result;
				msg.obj = resultdata;
				LogManager.LogShow("SigninGetPwdSecretQuestionActivity", "Callback" + result);
				if (TpAuth.AUTH_GETPWD_OK == result) {
					LogManager.LogShow("SigninGetPwdSecretQuestionActivity", "AUTH_GETPWD_OK");
					handler.sendMessage(msg);
				} else if (TpAuth.AUTH_GETPWD_ERR == result) {
					LogManager.LogShow("SigninGetPwdSecretQuestionActivity", "AUTH_GETPWD_ERR");
					handler.sendMessage(msg);
				} else if (TpAuth.AUTH_GETPWD_TIMEOUT == result) {
					LogManager.LogShow("SigninGetPwdSecretQuestionActivity", "AUTH_GETPWD_TIMEOUT");
					handler.sendMessage(msg);
				}
			}
		});
	}

//	private void showToast(String str) {
//		Toast.makeText(SigninGetPwdSecretQuestionActivity.this, str, Toast.LENGTH_SHORT).show();
//	}
}
